cmake_minimum_required(VERSION 3.22)
project(vdb VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 查找src下所有.cc文件
file(GLOB_RECURSE SRC_FILES src/*.cc)
# 创建静态库，包含所有源文件
add_library(vdblib STATIC ${SRC_FILES})
target_include_directories(vdblib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
find_library(FAISS_LIB faiss HINTS /usr/local/lib)
find_path(FAISS_INCLUDE_DIR faiss/Index.h HINTS /usr/local/include)

if(NOT FAISS_LIB)
    message(FATAL_ERROR "Faiss library not found")
endif()

if(NOT FAISS_INCLUDE_DIR)
    message(FATAL_ERROR "Faiss headers not found")
endif()

message(STATUS "Found Faiss: ${FAISS_LIB}")
message(STATUS "Faiss include: ${FAISS_INCLUDE_DIR}")


# 添加 OpenBLAS（Faiss 依赖）
# find_package(OpenBLAS REQUIRED)

# 包含 Faiss 头文件
include_directories(/usr/local/include /usr/include)

# 查找spdlog
find_package(spdlog REQUIRED)

# 查找rocksdb
find_path(ROCKSDB_INCLUDE_DIR rocksdb/db.h
          PATHS /usr/include)

find_library(ROCKSDB_LIBRARY
             NAMES rocksdb
             PATHS /usr/lib/x86_64-linux-gnu)

if(ROCKSDB_INCLUDE_DIR AND ROCKSDB_LIBRARY)
    message(STATUS "Found RocksDB: ${ROCKSDB_LIBRARY}")
else()
    message(FATAL_ERROR "Could not find RocksDB")
endif()

find_path(ROARING_INCLUDE_DIR roaring/roaring.h
          PATHS /usr/local/include /usr/include)

find_library(ROARING_LIBRARY
             NAMES roaring
             PATHS /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu)

if(ROARING_INCLUDE_DIR AND ROARING_LIBRARY)
    message(STATUS "Found Roaring Bitmap: ${ROARING_LIBRARY}")
else()
    message(FATAL_ERROR "Could not find Roaring Bitmap. Please install libroaring-dev or build from https://github.com/RoaringBitmap/CRoaring")
endif()

find_path(NURAFT_INCLUDE_DIR libnuraft/nuraft.hxx
          PATHS /usr/local/include /usr/include)

find_library(NURAFT_LIBRARY
             NAMES nuraft
             PATHS /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu)

if(NURAFT_INCLUDE_DIR AND NURAFT_LIBRARY)
    message(STATUS "Found NURAFT: ${NURAFT_LIBRARY}")
else()
    message(FATAL_ERROR "Could not find NURAFT. build from https://gitee.com/mirrors_eBay/NuRaft.git")
endif()

# 创建可执行文件
add_executable(vdb_server src/vdb_server.cpp)
target_include_directories(vdb_server PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${ROCKSDB_INCLUDE_DIR} ${ROARING_INCLUDE_DIR} ${NURAFT_INCLUDE_DIR})
# 链接库文件
target_link_libraries(vdblib PRIVATE  ${FAISS_LIB} ${ROARING_LIBRARY} ${NURAFT_LIBRARY})
target_link_libraries(vdb_server PRIVATE  ${FAISS_LIB} vdblib spdlog::spdlog ${ROCKSDB_LIBRARY} ${ROARING_LIBRARY} ${NURAFT_LIBRARY})

option(BUILD_TESTS "Build unit tests" ON)
if(BUILD_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif()